﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;

namespace EntityDaoLibrary
{
    public class RegisterInfoDAO : IEntityDAO
    {
        private IDataAccess data;
        public RegisterInfoDAO()
        { }

        public RegisterInfoDAO(IDataAccess data)
        {
            this.data = data;
        }

        #region IEntityDAO 成员

        public void insertEntity(System.Data.DataSet entityData)
        {
            throw new NotImplementedException();
        }

        public void deleteEntity(System.Data.DataSet entityData)
        {
            throw new NotImplementedException();
        }

        public void updateEntity(System.Data.DataSet entityData)
        {
            throw new NotImplementedException();
        }

        public System.Data.DataSet FindByPrimaryKey(object strKeyValue)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// 获取病人登记信息实体
        /// </summary>
        /// <param name="inhosID"></param>
        /// <param name="syxh"></param>
        /// <param name="centerid"></param>
        /// <returns></returns>
        public DataSet GetRegistInfo(string inhosID, string syxh, int centerid)
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();
                QueryParameterCollection para;
                DataSet ds = new DataSet();

                if (!inhosID.Equals(string.Empty))
                {
                    //sql = "select a.* ,b.*,c.* from ZY_BRSYK  a,ZY_BRZDQK  b,BQ_BRCWXXK  c,ZY_BRJSK d where a.syxh=b.syxh and a.syxh=c.syxh and a.syxh=d.syxh and a.blh=@blh";
                    sql = @"SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh WHERE A.brzt NOT IN (3,9) AND A.ybdm=15 AND A.blh=@blh";
                    para = new QueryParameterCollection(1);
                    para.Add("@blh", inhosID);
                }
                if (!syxh.Equals(string.Empty))
                {
                    sql = "select a.* ,b.*,c.* from ZY_BRSYK  a,ZY_BRZDQK  b,BQ_BRCWXXK  c,ZY_BRJSK d where a.syxh=b.syxh and a.syxh=c.syxh and a.syxh=d.syxh and a.syxh=@syxh";
                    para = new QueryParameterCollection(1);
                    para.Add("@syxh", syxh);
                }
                if (centerid != 0)
                {
                    sql = "select a.* ,b.*,c.* from ZY_BRSYK  a,ZY_BRZDQK  b,BQ_BRCWXXK  c,ZY_BRJSK d where a.syxh=b.syxh and a.syxh=c.syxh and a.syxh=d.syxh and centerid=@centerid";
                    para = new QueryParameterCollection(1);
                    para.Add("@centerid", centerid);
                }

                data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }

        }

        //2009.04.17 修改
        public DataSet FindAllMedicarePatientInfo()
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();
                //QueryParameterCollection para;
                DataSet ds = new DataSet();

                //sql = "select a.* ,b.*,c.* from ZY_BRSYK  a,ZY_BRZDQK  b,BQ_BRCWXXK  c,ZY_BRJSK d where a.syxh=b.syxh and a.syxh=c.syxh and a.syxh=d.syxh and a.brlx=888 and d.jszt=0";
                //sql = "SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh WHERE A.brzt NOT IN (3,9) AND A.ybdm=15";
//                sql = @"SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh 
//                        WHERE A.brzt!=9 AND  A.ybdm=15 AND B.jlzt=0   ";
                sql = @"select aa.* from I_RALATIONINHOSREG  as i,(
                        SELECT A.blh ,B.hzxm FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh 
                                                WHERE A.brzt!=9 AND  A.ybdm=15 AND B.jlzt=0 ) as aa
                        where i.INHOSID=aa.blh and i.FLAG='2'";
                data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        public DataSet FindbyRegistedPatientInfo()
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();

                DataSet ds = new DataSet();
                //过滤状态为2的已结算的病人  2010-05-27 
                sql = @"SELECT TOP 200 I_RALATIONINHOSREG.*,ZY_BRSYK.hzxm FROM I_RALATIONINHOSREG INNER JOIN ZY_BRSYK 
                        ON I_RALATIONINHOSREG.INHOSID=ZY_BRSYK.blh WHERE I_RALATIONINHOSREG.FLAG !='2' ORDER BY TRANDT DESC
                        ";

                data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

       /// <summary>
        /// 查询已结算的病人 2011.02.25
       /// </summary>
       /// <param name="strWhere">查询条件</param>
       /// <returns></returns>
        public DataSet FindbyRegistedPatientInfo(string strWhere )
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();

                DataSet ds = new DataSet();
                //过滤状态为2的已结算的病人  2010-05-27 
                sql = @"SELECT  I_RALATIONINHOSREG.*,ZY_BRSYK.hzxm FROM I_RALATIONINHOSREG INNER JOIN ZY_BRSYK 
                        ON I_RALATIONINHOSREG.INHOSID=ZY_BRSYK.blh ";
                sql += string.IsNullOrEmpty(strWhere) ? "" : "  Where " + strWhere;
                sql+="ORDER BY TRANDT DESC ";

                data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        /// <summary>
        /// 根据时间段查询已经结算过的病人
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public DataSet FindbyRegistedPatientInfo(DateTime beginTime, DateTime endTime)
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();
                QueryParameterCollection para;
                DataSet ds = new DataSet();

                sql = @"SELECT  I_RALATIONINHOSREG.*,ZY_BRSYK.hzxm FROM I_RALATIONINHOSREG INNER JOIN ZY_BRSYK 
                        ON I_RALATIONINHOSREG.INHOSID=ZY_BRSYK.blh WHERE I_RALATIONINHOSREG.FLAG = '2' AND I_RALATIONINHOSREG.TRANDT between @beginTime AND @endTime ORDER BY TRANDT 
                        ";
                para = new QueryParameterCollection(2);
                para.Add("@beginTime", beginTime);
                para.Add("@endTime", endTime);
                data.ExecuteDataset(sql, para, ds);
                // data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        //根据住院号查询该病人信息
        public DataSet FindbyRegistedPatientInfoByInhosID(string inhosid)
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();
                QueryParameterCollection para;

                DataSet ds = new DataSet();

                sql = @"SELECT I_RALATIONINHOSREG.*,ZY_BRSYK.hzxm FROM I_RALATIONINHOSREG INNER JOIN ZY_BRSYK 
                        ON I_RALATIONINHOSREG.INHOSID=ZY_BRSYK.blh WHERE INHOSID=@INHOSID ORDER BY TRANDT 
                        ";
                para = new QueryParameterCollection(1);
                para.Add("@INHOSID", inhosid);
                data.ExecuteDataset(sql, para, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        public DataSet FindbyUnRegistPaientInfo()
        {
            try
            {
                string sql = "";
                SqlStruct sqlStruct = new SqlStruct();
                DataSet ds = new DataSet();

                //sql = "select a.* ,b.*,c.* from ZY_BRSYK  a,ZY_BRZDQK  b,BQ_BRCWXXK  c,ZY_BRJSK d where a.syxh=b.syxh and a.syxh=c.syxh and a.syxh=d.syxh and a.brlx=888 and d.jszt=0 and a.syxh not in (SELECT INHOSID FROM I_RALATIONINHOSREG )";
                //sql = "SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh WHERE A.brzt NOT IN (3,9) AND A.ybdm=15 AND A.blh NOT IN (SELECT INHOSID FROM I_RALATIONINHOSREG )";
                //                sql = @"SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh 
                //                        WHERE A.brzt!=9 AND  A.ybdm=15 AND B.jlzt=0 AND B.jszt!=0 AND A.blh NOT IN (SELECT INHOSID FROM I_RALATIONINHOSREG )";
                sql = @"SELECT A.* ,B.* FROM ZY_BRSYK AS A inner join ZY_BRJSK AS B ON A.blh=B.blh 
                        WHERE A.brzt!=9 AND  A.ybdm=15 AND B.jlzt=0 AND A.blh NOT IN (SELECT INHOSID FROM I_RALATIONINHOSREG )";
                data.ExecuteDataset(sql, ds);

                return ds;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }

        }

    }
}
